package com.neusoft.controller;

import java.util.List;

import com.neusoft.po.CommonResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.core.annotation.Order;
import org.springframework.web.bind.annotation.*;

import com.neusoft.po.Credit;
import com.neusoft.po.Orders;
import com.neusoft.service.CreditService;

@RestController
@RequestMapping("/CreditController")
@RefreshScope
public class CreditController {
	@Autowired
	CreditService creditService;

	//查询可用积分
	@GetMapping("/getUsableCredit/{userId}")
	public CommonResult<Integer> getUsableCredit(@PathVariable("userId") String userId) {

		int result = creditService.getUsableCredit(userId);
		return new CommonResult<>(200, "success", result);
	}
	//查询积分流水
	@GetMapping("/getCreditTransaction/{userId}")
	public CommonResult<List<Credit>> getCreditTransaction(@PathVariable("userId") String userId) {
		List<Credit> result = creditService.getCreditTransaction(userId);
		return new CommonResult<>(200, "success", result);
	}

	//待支付订单调用，获取抵扣数额
	@GetMapping("/ordersGetDiscount/{userId}")
	public CommonResult<Double> ordersGetDiscount(@PathVariable("userId") String userId)
	{
		Double result = creditService.ordersGetDiscount(userId);
		return new CommonResult<>(200, "success", result);
	}

	//支付订单后调用，根据成功的订单增加积分
	@PutMapping("/ordersAddCredit/{userId}/{orderDate}/{orderTotal}")
	public CommonResult<Integer> ordersAddCredit(@PathVariable("userId") String userId,
												 @PathVariable("orderDate") String orderDate,
												 @PathVariable("orderTotal") Double orderTotal) {
		Orders orders = new Orders();
		orders.setUserId(userId);
		orders.setOrderDate(orderDate);
		orders.setOrderTotal(orderTotal);

		int result = creditService.ordersAddCredit(orders);
		return new CommonResult<>(200, "success", result);
	}

	//支付订单后调用，减去使用的积分
	//返回值：0->失败;>0->成功
	@PutMapping("/ordersUseDiscount/{userId}")
	public CommonResult<Integer> ordersUseDiscount(@PathVariable("userId") String userId) {

		Orders orders = new Orders();
		orders.setUserId(userId);
		int result = creditService.ordersUseDiscount(orders);
		return new CommonResult<>(200, "success", result);
	}
}
